package de.uni.kassel.campusappver3.view;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TimePicker;
import android.widget.Toast;
import de.uni.kassel.campusappver3.MainActivity;
import de.uni.kassel.campusappver3.R;
import de.uni.kassel.campusappver3.crawler.ICrawl;
import de.uni.kassel.campusappver3.crawler.NVVCrawler;
import de.uni.kassel.campusappver3.model.CrawlerReturnValue;
import de.uni.kassel.campusappver3.model.Resources;

public class BahnActivity extends Activity implements ICrawl{
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bahn);
		
		//get current time
		Calendar c = Calendar.getInstance();
		int hours = c.get(Calendar.HOUR_OF_DAY);
		int minutes = c.get(Calendar.MINUTE);
		String time = "";
		
		if (hours < 10) {
			time += "0";
		}
		time += hours + ":";
		if (minutes < 10) {
			time += "0";
		}
		time += minutes;

		//in portrait mode
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			setContentView(R.layout.activity_bahn);
			TimePicker picker = (TimePicker) findViewById(R.id.timePicker1);
			picker.setIs24HourView(true);
			picker.setCurrentHour(hours);
			
			final BahnActivity thisActivity = this;
			Button search = (Button) findViewById(R.id.button1);
			search.setOnClickListener(new OnClickListener() {
	 
				@Override
				public void onClick(View arg0) {
					ListView listView = (ListView) findViewById(R.id.listViewBahn);
					listView.setAdapter(null);
					
					NVVCrawler crawler = new NVVCrawler(thisActivity);
					TimePicker picker = (TimePicker) findViewById(R.id.timePicker1);
					int hours = picker.getCurrentHour();
					int minutes = picker.getCurrentMinute();
					
					String time = "";
					
					if (hours < 10) {
						time += "0";
					}
					time += hours + ":";
					if (minutes < 10) {
						time += "0";
					}
					time += minutes;
					
					Object[] parms = {time};
					crawler.fetch(parms);
				}
	 
			});
			
		//landscape mode shows only the list
		} else {
			setContentView(R.layout.activity_bahn_ver);
		}
		
		//see MensaCrawler.java for further information about this construction
		if(Resources.getInstance().getTrainInfo() == null){
			Object[] parms = {time};
        	NVVCrawler nvv = new NVVCrawler(this);
        	if (!MainActivity.internetConnection) {
        		createUI();
        		Toast.makeText(getApplicationContext(), "Keine Internetverbindung.", Toast.LENGTH_SHORT).show();
        		ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBarBahn);
        		progressBar.setVisibility(View.INVISIBLE);
        	} else {
        		nvv.fetch(parms);
        	}
        } else {
        	createUI();
        }
	}
	
	@Override
	public void onCrawlFinish(CrawlerReturnValue crv) {
		Resources.getInstance().setTrainInfo(crv);
		createUI();
	}
	
/**
 * Creates the UI (fills the list with information, hiding
 * progressbar etc)
 */
	private void createUI() {
		
		if (!MainActivity.internetConnection) {
			return;
		}
		
		CrawlerReturnValue crv = Resources.getInstance().getTrainInfo();
		ListView listView = (ListView) findViewById(R.id.listViewBahn);
		ArrayAdapter<String> adapter;
		ArrayList<String> listItems = new ArrayList<String>();
		
		for (String key : crv.getKeys()) {	
			
		
			String info = crv.getValue(key).get(0) + 
					  " " + crv.getValue(key).get(1) +
					  " " + crv.getValue(key).get(2);
			
			if (listItems.size() == 0) {
				listItems.add(info);
			} else {
				int myTime = getTime(crv.getValue(key).get(0));
				int position = 0;

				for (String item : listItems) {
					if (myTime < getTime(item)) {
						break;
					}
					position++;
				}
					
				listItems.add(position, info);
			}
			
			
		}
		
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
	    listView.setAdapter(adapter);
	    ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBarBahn);
	    progressBar.setVisibility(View.INVISIBLE);
	    
	    if (!MainActivity.internetConnection) {
        	Toast.makeText(getApplicationContext(), "Keine Internetverbindung.", Toast.LENGTH_SHORT).show();
        }
	}
/**
 * Helper method needed to sort the list
 * @param string
 * @return
 */
	private int getTime(String string) {
		int hours = Integer.parseInt(string.substring(0, 2).trim());
		int minutes = Integer.parseInt(string.substring(3, 5).trim());
		return hours * 60 + minutes;
	}
}
